/* Copyright 2008 Bas van den Berg
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "yaffut.h"
#include "myassert.h"
#include "TestSession.h"

#include "Area.h"
#include "MudPlayer.h"
#include "Error.h"
#include "Room.h"
#include "MudExit.h"

using namespace Chronos;
using namespace std;


class AreaTest {
public:
    AreaTest()
        : session("player1")
		, area(0, "")
        , player(session.getPlayer())
    {
        loadArea();
    }
    void loadArea() {
        room1 = new Room("North room", "");
        room1->addObject(new MudSouthExit(area, 2));
        area.addRoom(1, *room1);

        room2 = new Room("South room", "");
        room2->addObject(new MudNorthExit(area, 1));
        area.addRoom(2, *room2);
    }

    TestSession session; 
    Room* room1;
    Room* room2;
	Area area;
    MudPlayer& player;
};


TEST(AreaTest, testShouldAddAndRemovePlayer) {
    area.addPlayer(player, 2, "");
    ASSERT_NULL(room1->findObject("player1"));
    ASSERT_NOT_NULL(room2->findObject("player1"));
	room2->removePlayer(player, "");
    ASSERT_NULL(room2->findObject("player1"));
}


TEST(AreaTest, testAddingPlayerToNonExistingRoom) {
    try {
        area.addPlayer(player, 10, "");
        ASSERT_FAIL();
    } catch (MudError& e) {
        ASSERT_STR_EQUAL("Area: cannot find dest room 10", e.what());
    }
}


TEST(AreaTest, testMovingPlayer) {
    area.addPlayer(player, 2, "");
    area.movePlayer(player, 1, "leaves", "arrives");
    ASSERT_NOT_NULL(room1->findObject("player1"));
    ASSERT_NULL(room2->findObject("player1"));
	room1->removePlayer(player, "");
}

